<?php


namespace app\api\controller;

/**
 * Class 交易订单相关定时器
 * @package app\api\controller
 */
class Ordertime extends \app\common\controller\Api
{
    protected $noNeedLogin = ['*'];
    /**
     * 订单超时
     */
    public function order_over(){
        $list = db('order')
            ->where('status','1')
            ->where('overtime','<',time())
            ->limit(1000)
            ->select();

        foreach ($list as $key => $value){
            //超时不付款
            if ($value['status'] == 1){
                if ($value['type'] == 1){
                    //cb
                    db('user')
                        ->where('id',$value['sell_user_id'])
                        ->inc('cb_num',$value['all_freeze'])
                        ->dec('cb_freeze',$value['all_freeze'])
                        ->update();
                    setCbwater($value['sell_user_id'],$value['all_freeze'],1,'交易超时退回'.$value['all_freeze']);//记录流水
                }else{
                    //cc
                    db('user')
                        ->where('id',$value['sell_user_id'])
                        ->inc('cc_num',$value['all_freeze'])
                        ->dec('cc_freeze',$value['all_freeze'])
                        ->update();
                    setCcwater($value['sell_user_id'],$value['all_freeze'],1,'交易超时退回'.$value['all_freeze']);//记录流水
                }
                db('order')->where('id',$value['id'])->update(['status'=>6,'updatetime'=>time()]);
            }else{
                //超时不收款
                if ($value['type'] == 1){
                    //cb
                    db('user')
                        ->where('id',$value['sell_user_id'])
                        ->dec('cb_freeze',$value['all_freeze'])
                        ->update();
                    db('user')
                        ->where('id',$value['buy_user_id'])
                        ->inc('cb_num',$value['num'])
                        ->update();
                    setCbwater($value['sell_user_id'],$value['all_freeze'],1,'自动交易'.$value['all_freeze']);//记录流水
                    setCbwater($value['buy_user_id'],$value['num'],1,'订单交易'.$value['num']);//记录流水
                }else{
                    //cc
                    db('user')
                        ->where('id',$value['sell_user_id'])
                        ->dec('cc_freeze',$value['all_freeze'])
                        ->update();
                    db('user')
                        ->where('id',$value['buy_user_id'])
                        ->inc('cc_num',$value['num'])
                        ->update();
                    setCcwater($value['sell_user_id'],$value['all_freeze'],1,'自动交易'.$value['all_freeze']);//记录流水
                    setCcwater($value['buy_user_id'],$value['num'],1,'订单交易'.$value['num']);//记录流水
                }
                db('order')->where('id',$value['id'])->update(['status'=>3,'updatetime'=>time()]);
            }

        }
    }
}